class Codec:

    def serialize(self, root):
        """Encodes a tree to a single string.
        
        :type root: TreeNode
        :rtype: str
        """
        path = []

        if root is None:
            return "[]"
        
        queue = collections.deque()
        queue.append(root)
        while len(queue) > 0:
            node = queue.popleft()
            if node != None:
                path.append(str(node.val))
                queue.append(node.left)
                queue.append(node.right)
            else:
                path.append("null")
        
        #去掉末尾连续null
        while path[-1] == "null":
            path.pop()
        return "[" + ",".join(path) + "]"                       


    def deserialize(self, data):
        """Decodes your encoded data to tree.
        
        :type data: str
        :rtype: TreeNode
        """
        if data == "[]":
            return None

        data = data[1:-1].split(",")
        
        index = 0
        root = TreeNode(int(data[index]))
        index += 1
        
        queue = collections.deque()
        queue.append(root)
        while index < len(data):
            node = queue.popleft()
            
            #左节点
            if data[index] == 'null':
                node.left = None
            else:
                node.left = TreeNode(int(data[index]))
                queue.append(node.left)
            index += 1
            #右节点
            if index < len(data):
                if data[index] == 'null':
                    node.right = None
                else:
                    node.right =  TreeNode(int(data[index]))
                    queue.append(node.right)
            index += 1
        return root
